import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemNotifyMessageApi } from '#/api/system/notify/message';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { $t } from '#/locales';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { formatDateTime } from '@vben/utils';

import { DictTag } from '#/components/dict-tag';
import { getRangePickerDefaultProps } from '#/utils';

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'userId',
      label: $t('views.system.notify.logs.userId'),
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: $t('views.system.notify.logs.userIdPlaceholder'),
      },
    },
    {
      fieldName: 'userType',
      label: $t('views.system.notify.logs.userType'),
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.USER_TYPE, 'number'),
        placeholder: $t('views.system.notify.logs.userTypePlaceholder'),
      },
    },
    {
      fieldName: 'templateCode',
      label: $t('views.system.notify.template.code'),
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: $t('views.system.notify.template.codePlaceholder'),
      },
    },
    {
      fieldName: 'templateType',
      label: $t('views.system.notify.template.type'),
      component: 'Select',
      componentProps: {
        options: getDictOptions(
          DICT_TYPE.SYSTEM_NOTIFY_TEMPLATE_TYPE,
          'number',
        ),
        allowClear: true,
        placeholder: $t('views.system.notify.template.typePlaceholder'),
      },
    },
    {
      fieldName: 'createTime',
      label: $t('common.createTime'),
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'id',
      title: $t('views.common.id'),
      minWidth: 100,
    },
    {
      field: 'userType',
      title: $t('views.system.notify.logs.userType'),
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.USER_TYPE },
      },
    },
    {
      field: 'userId',
      title: $t('views.system.notify.logs.userId'),
      minWidth: 100,
    },
    {
      field: 'templateCode',
      title: $t('views.system.notify.template.code'),
      minWidth: 120,
    },
    {
      field: 'templateNickname',
      title: $t('views.system.notify.template.nickname'),
      minWidth: 180,
    },
    {
      field: 'templateContent',
      title: $t('views.system.notify.template.content'),
      minWidth: 200,
    },
    {
      field: 'templateParams',
      title: $t('views.system.notify.template.templateParams'),
      minWidth: 180,
      formatter: ({ cellValue }) => {
        try {
          return JSON.stringify(cellValue);
        } catch {
          return '';
        }
      },
    },
    {
      field: 'templateType',
      title: $t('views.system.notify.template.type'),
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.SYSTEM_NOTIFY_TEMPLATE_TYPE },
      },
    },
    {
      field: 'readStatus',
      title: $t('views.system.notify.logs.readStatus'),
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.INFRA_BOOLEAN_STRING },
      },
    },
    {
      field: 'readTime',
      title: $t('views.system.notify.logs.readTime'),
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      field: 'createTime',
      title: $t('common.createTime'),
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      title: $t('common.action'),
      width: 120,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

/** 详情页的字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      field: 'id',
      label: $t('views.common.id'),
    },
    {
      field: 'userType',
      label: $t('views.system.notify.logs.userType'),
      content: (data: SystemNotifyMessageApi.NotifyMessage) => {
        return h(DictTag, {
          type: DICT_TYPE.USER_TYPE,
          value: data?.userType,
        });
      },
    },
    {
      field: 'userId',
      label: $t('views.system.notify.logs.userId'),
    },
    {
      field: 'templateId',
      label: $t('views.system.notify.template.id'),
    },
    {
      field: 'templateCode',
      label: $t('views.system.notify.template.code'),
    },
    {
      field: 'templateNickname',
      label: $t('views.system.notify.template.nickname'),
    },
    {
      field: 'templateContent',
      label: $t('views.system.notify.template.content'),
    },
    {
      field: 'templateParams',
      label: $t('views.system.notify.template.templateParams'),
      content: (data: SystemNotifyMessageApi.NotifyMessage) => {
        try {
          return JSON.stringify(data?.templateParams);
        } catch {
          return '';
        }
      },
    },
    {
      field: 'templateType',
      label: $t('views.system.notify.template.type'),
      content: (data: SystemNotifyMessageApi.NotifyMessage) => {
        return h(DictTag, {
          type: DICT_TYPE.SYSTEM_NOTIFY_TEMPLATE_TYPE,
          value: data?.templateType,
        });
      },
    },
    {
      field: 'readStatus',
      label: $t('views.system.notify.logs.readStatus'),
      content: (data: SystemNotifyMessageApi.NotifyMessage) => {
        return h(DictTag, {
          type: DICT_TYPE.INFRA_BOOLEAN_STRING,
          value: data?.readStatus,
        });
      },
    },
    {
      field: 'readTime',
      label: $t('views.system.notify.logs.readTime'),
      content: (data: SystemNotifyMessageApi.NotifyMessage) => {
        return formatDateTime(data?.readTime || '') as string;
      },
    },
    {
      field: 'createTime',
      label: $t('common.createTime'),
      content: (data: SystemNotifyMessageApi.NotifyMessage) => {
        return formatDateTime(data?.createTime || '') as string;
      },
    },
  ];
}
